package cn.ming.wfchat.app.me.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import cn.ming.wfchat.R;
import cn.ming.wfchat.app.AppService;
import cn.ming.wfchat.app.me.activity.adapter.RedPageListAdapter;
import cn.ming.wfchat.app.me.activity.model.RedPageListBean;
import cn.ming.wfchat.app.utils.ImageLoaderUtil;
import cn.ming.wfchat.app.utils.StatusBarUtil;
import cn.ming.wfchat.kit.utils.Tools;
import cn.wildfirechat.remote.ChatManager;

/**
 * 红包
 */
public class RedPageListActivity extends AppCompatActivity {
    String hash;
    @BindView(R.id.iv_img)
    ImageView ivImg;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_slogn)
    TextView tvSlogn;
    @BindView(R.id.tv_total_amount)
    TextView tvTotalAmount;
    @BindView(R.id.tv_cny)
    TextView tvCny;
    @BindView(R.id.tv_context)
    TextView tvContext;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.ll_layout)
    LinearLayout ll_layout;
    private RedPageListAdapter redPageListAdapter;
    private RedPageListBean redPageListBean;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_red_page_list);
        StatusBarUtil.setColor(this, getResources().getColor(R.color.red0));
        ButterKnife.bind(this);
        hash = getIntent().getStringExtra("hash");
        initDate();

    }

    void initDate() {
        redPageListBean = new RedPageListBean();
        redPageListAdapter = new RedPageListAdapter(redPageListBean.getList());
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(redPageListAdapter);
        AppService.Instance().findLog(hash, new AppService.RedPageListCallback() {
            @Override
            public void onUiSuccess(RedPageListBean rBean) {
                redPageListBean = rBean;
                ImageLoaderUtil.display(ivImg, rBean.getRedPacketOrder().getPortrait());
                tvName.setText(rBean.getRedPacketOrder().getDisplayName() + "的红包");
                tvSlogn.setText(rBean.getRedPacketOrder().getSlogan());
                redPageListAdapter.setNewData(rBean.getList());
                for (RedPageListBean.ListBean listBean : rBean.getList()) {
                    if (listBean.getTakeUserId().equals(ChatManager.Instance().getUserId())) {
                        ll_layout.setVisibility(View.VISIBLE);
                        tvTotalAmount.setText(listBean.getAmount() + "");
                        tvCny.setText("≈" + listBean.getCnyAmount() + "CNY");
                    }
                }
                tvContext.setText("已经领取" + rBean.getList().size() + "/" + rBean.getRedPacketOrder().getNumber() + "个," +
                        "共" + rBean.getRedPacketOrder().getReceiveAmount() + "/" + rBean.getRedPacketOrder().getAmount() + "FJ");
            }

            @Override
            public void onUiFailure(int code, String msg) {
                Tools.showToast(RedPageListActivity.this, msg);
            }
        });
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        ll_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Tools.jump(RedPageListActivity.this, MyAssetsActivity.class, false);
            }
        });


    }
}
